package znbkBkfx.BkfxAnswerSheet;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.znbk.znbklibrary.util.LancooUtils;
import com.example.znbk.znbklibrary.widget.ScaleButton;
import com.lancoo.znbkxx.R;

import znbkBkfx.GetBkfxJson.BkfxPaperEntity;


public class ShowAnswerSheetDialog extends AlertDialog {

    /**
     * 一个参数的构造方法
     * @param context 上下文对象
     */
    private Context context;
    private BkfxPaperEntity bkfxPaperEntity;
    public ShowAnswerSheetDialog(Context context, BkfxPaperEntity bkfxPaperEntity) {
        super(context);
        this.context = context;
        this.bkfxPaperEntity = bkfxPaperEntity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_answersheetview);
        //设置背景透明，不然会出现白色直角问题
        Window window = getWindow();
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setCanceledOnTouchOutside(false);
        //初始化布局控件
        initView();
        //确定和取消按钮的事件监听
        initEvent();
        //设置参数必须在show之后，不然没有效果
        WindowManager.LayoutParams params = getWindow().getAttributes();
        getWindow().setAttributes(params);
    }

    private ScaleButton mBtnSDialogClose;
    private LinearLayout mLlAnswerSheet;
    private void initView(){
        mBtnSDialogClose = findViewById(R.id.sbtn_dialogClose);
        mBtnSDialogClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        mLlAnswerSheet = findViewById(R.id.ll_answerSheet);
        showQuesTypeAndItems();

    }

    private void initEvent(){

    }

    private TextView[] mTvQuesIndexTypeScore;
    private RelativeLayout[] mRlQuesList;
    private QuesIndexView[] mSBtnQuesItems;
    private void showQuesTypeAndItems() {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;         // 屏幕宽度（像素）
        int height = dm.heightPixels;       // 屏幕高度（像素）

        int childListSize = bkfxPaperEntity.getQuesList().size();
        mTvQuesIndexTypeScore = new TextView[childListSize];
        mRlQuesList = new RelativeLayout[childListSize];
        for (int i = 0; i < childListSize; i++) {

            mSBtnQuesItems = new QuesIndexView[childListSize];

            mTvQuesIndexTypeScore[i] = new TextView(context);
            RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT ,ViewGroup.LayoutParams.WRAP_CONTENT);
            mTvQuesIndexTypeScore[i].setPadding(40,20,0,20);
            mTvQuesIndexTypeScore[i].setBackgroundColor(Color.parseColor("#e2eff4"));
            String GenreName = bkfxPaperEntity.getQuesList().get(i).getGenreInfo().getGenreName();
            String TypeName = bkfxPaperEntity.getQuesList().get(i).getTypeInfo().getTypeName();
            if(GenreName.equals("")){
                GenreName = TypeName;
            }
            mTvQuesIndexTypeScore[i].setText(LancooUtils.numberToChinese(i+1)+"、"+GenreName);
            mLlAnswerSheet.addView(mTvQuesIndexTypeScore[i],rl);

            mRlQuesList[i] = new RelativeLayout(context);
            mRlQuesList[i].setPadding(30, 30, 30, 30);

            int subChildListSize = -1;//小题个数
            String TypeNo = bkfxPaperEntity.getQuesList().get(i).getTypeInfo().getTypeNo();
            if(TypeNo.equals("N")||TypeNo.equals("Q")||TypeNo.equals("b")||TypeNo.equals("U")||TypeNo.equals("S")||TypeNo.equals("m")){
                subChildListSize = bkfxPaperEntity.getPaperAnswer().get(i).getChildList().get(0).getSubChildList().get(0).getSubChildItem().size();
            }else {
                subChildListSize = bkfxPaperEntity.getPaperAnswer().get(i).getChildList().size();
            }
            ScaleButton[] SBtn = new ScaleButton[subChildListSize];
            int k = -1;
            for (int j=0; j<subChildListSize; j++) {
                SBtn[j]=new ScaleButton(context);
                SBtn[j].setId(j);
                SBtn[j].setTag(j+1);    //为按钮设置一个标记，来确认是按下了哪一个按钮
                int SortIndex = -1;//小题个数
                if(TypeNo.equals("N")||TypeNo.equals("Q")||TypeNo.equals("b")||TypeNo.equals("U")||TypeNo.equals("S")||TypeNo.equals("m")){
                    SortIndex = bkfxPaperEntity.getQuesList().get(i).getChildList().get(0).getSubChildList().get(0).getSubChildItem().get(j).getSortIndex();
                }else {
                    SortIndex = bkfxPaperEntity.getQuesList().get(i).getChildList().get(j).getSubChildList().get(0).getSubChildItem().get(0).getSortIndex();
                }
                SBtn[j].setText(String.valueOf(SortIndex));
                SBtn[j].setTextSize(12);
                SBtn[j].setGravity(Gravity.CENTER);
                RelativeLayout.LayoutParams btParams = new RelativeLayout.LayoutParams ((width)/8,(width)/8);

                int SubmitState = bkfxPaperEntity.getPaperAnswer().get(i).getSubmitState();
                String StuAnswer = "";
                String ItemAnswer = "";
                if(TypeNo.equals("N")||TypeNo.equals("Q")||TypeNo.equals("b")||TypeNo.equals("U")||TypeNo.equals("S")||TypeNo.equals("m")){
                     StuAnswer  = bkfxPaperEntity.getPaperAnswer().get(i).getChildList().get(0).getSubChildList().
                            get(0).getSubChildItem().get(j).getStuAnswer();
                     ItemAnswer = bkfxPaperEntity.getPaperAnswer().get(i).getChildList().get(0).getSubChildList().
                            get(0).getSubChildItem().get(j).getItemAnswer();
                }else {
                     StuAnswer  = bkfxPaperEntity.getPaperAnswer().get(i).getChildList().get(j).getSubChildList().
                            get(0).getSubChildItem().get(0).getStuAnswer();
                     ItemAnswer = bkfxPaperEntity.getPaperAnswer().get(i).getChildList().get(j).getSubChildList().
                            get(0).getSubChildItem().get(0).getItemAnswer();
                }

                if(SubmitState!=2){
                    if(StuAnswer.equals("")){
                        SBtn[j].setBackgroundResource(R.mipmap.white);
                    }else{
                        SBtn[j].setBackgroundResource(R.mipmap.blue);
                    }
                }else {
                    if (ItemAnswer.equals(StuAnswer)) {
                        SBtn[j].setBackgroundResource(R.mipmap.green);
                    } else {
                        SBtn[j].setBackgroundResource(R.mipmap.red);
                    }
                }

                if (j%5 == 0) {
                    k++;
                }
                btParams.setMargins(30,20,20,20);
                btParams.leftMargin = ((width)/7+40)*(j%5); //横坐标定位
                btParams.topMargin = ((width)/7)*k+20; //纵坐标定位
                mRlQuesList[i].addView(SBtn[j],btParams); //将按钮放入layout组件
            }
            mLlAnswerSheet.addView(mRlQuesList[i]);
        }
    }

}